/**
 * 安卓白金英雄坛制作组
 * <br>
 * 文件名：BLGGame.java
 * <br>
 * 创建时间：2013-5-19 下午10:38:39
 * <br>
 * 所属项目：GmudEX
 * <br>
 *
 * @author 12548
 *         <br>
 */
package lostland.gmud.exv2.blgframework.impl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.tencent.stat.StatService;

import java.util.ArrayList;
import java.util.Arrays;

import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.LoadingScreen;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.MapTile;
import lostland.gmud.exv2.blgframework.Audio;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.blgframework.IFileIO;
import lostland.gmud.exv2.blgframework.IGame;
import lostland.gmud.exv2.blgframework.IGraphics;
import lostland.gmud.exv2.blgframework.IInput;
import lostland.gmud.exv2.ui.core.ButtonControlledScreen;
import lostland.gmud.exv2.ui.core.NewButton;


/**
 * 类名：BLGGame
 * <br>
 * 说明：
 *
 * @author 12548
 */
public abstract class BLGGame extends Activity implements IGame {

	private BLGFastRenderView renderView;
	protected IGraphics graphics;
	protected IInput input;
	protected IFileIO fileIO;
	public ArrayList<CScreen> scene = new ArrayList<>();
	protected WakeLock wakeLock;
	public Bitmap SecondFB, mapFB;
	protected Audio audio;

	public static float scaleX;
	public static float scaleY;

	public static float s2x, s2y;


	public static int height, width;

	public static final int FB2W = 640;
	public static final int FB2H = 384;

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		requestWindowFeature(Window.FEATURE_NO_TITLE);


		// boolean isLandscape= getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE;
		int frameBufferWidth = GameConstants.FBWIDTH;
		int frameBufferHeight = GameConstants.FBHEIGHT;

		// GmudGame.logError("GmudGame", "creating11");

		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight,
				Config.ARGB_8888);// ARGB8888 is instead

		mapFB = Bitmap.createBitmap(MapTile.TILE_WIDTH * MapScreen.C_ROWS, MapTile.TILE_WIDTH
				* MapScreen.C_COLUMNS, Config.ARGB_8888);// ARGB8888 is instead


		width = getWindowManager().getDefaultDisplay().getWidth();
		height = getWindowManager().getDefaultDisplay().getHeight();


		SecondFB = Bitmap.createBitmap(FB2W, FB2H, Config.ARGB_8888);// ARGB8888 is instead

		scaleX = (float) frameBufferWidth / width;
		scaleY = (float) frameBufferHeight / height;

		s2x = (float) FB2W / width;
		s2y = (float) FB2H / height;


		renderView = new BLGFastRenderView(this, frameBuffer);

		graphics = new BLGGraphics(getAssets(), frameBuffer, SecondFB, mapFB);

		fileIO = new BLGFileIO(this);
		audio = new BLGAudio(this);
		input = new BLGInput(this, renderView, scaleX, scaleY, s2x, s2y);// ^_^

		setContentView(renderView);


// PowerManager powerManager=(PowerManager)getSystemService(Context.POWER_SERVICE);
// wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GMUDGame");
	}

	@Override
	public void onResume() {
		super.onResume();
// wakeLock.acquire();
		getCurrentScreen().resume();
		CScreen scr = getCurrentScreen();
		if (scr instanceof ButtonControlledScreen) {
			Arrays.fill(NewButton.pressed, false);
			ButtonControlledScreen.refreshButtons();
		}
		renderView.resume();
		StatService.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
// wakeLock.release();
		renderView.pause();
		getCurrentScreen().pause();
		StatService.onPause(this);
		if (isFinishing()) {
			getCurrentScreen().dispose();
		}
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.IGame#getInput()
	 */
	@Override
	public IInput getInput() {
		return input;
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.IGame#getFileIO()
	 */
	@Override
	public IFileIO getFileIO() {
		return fileIO;
	}


	@Override
	public Audio getAudio() {
		return audio;
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.IGame#getGraphics()
	 */
	@Override
	public IGraphics getGraphics() {
		return graphics;
	}


	/**
	 * 请使用screen.removeFromGame来代理。
	 */
	public void popScreen() {

		CScreen scrb = getCurrentScreen();

		getCurrentScreen().npause();
		getCurrentScreen().dispose();

		scene.remove(scene.size() - 1);

		if (scene.isEmpty()) {
			scene.add(MapScreen.getInstance());
		}

		getCurrentScreen().nresume();
// getCurrentScreen().render(0);

		CScreen scr = getCurrentScreen();
		if (scr instanceof ButtonControlledScreen && !(scrb instanceof ButtonControlledScreen)) {
			Arrays.fill(NewButton.pressed, false);
			ButtonControlledScreen.refreshButtons();
		}
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.IGame#getCurrentScreeen()
	 */
	@Override
	public CScreen getCurrentScreen() {
		return scene.get(scene.size() - 1);
	}

	public CScreen getParentScreen() {
		if (scene.size() < 2)
			return MapScreen.getInstance();
		else
			return scene.get(scene.size() - 2);
	}

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.IGame#getStartScreen()
	 */
	@Override
	public CScreen getStartScreen() {
		return new LoadingScreen();
	}


	@Override
	public void oo() {
		finish();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	/*
	 * （非 Javadoc）
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
// System.out.println("onDestory");
		// if(!exit)
		// new SavingScreen(this).save();
		super.onDestroy();
	}

	/**
	 * @return renderView
	 */
	public BLGFastRenderView getRenderView() {
		if (renderView == null) {

			int frameBufferWidth = GameConstants.FBWIDTH;
			int frameBufferHeight = GameConstants.FBHEIGHT;

			// GmudGame.logError("GmudGame", "creating11");

			Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight,
					Config.ARGB_8888);// ARGB8888 is instead
			renderView = new BLGFastRenderView(this, frameBuffer);

			this.setContentView(renderView);
		}
		return renderView;
	}

	/**
	 * @param renderView 要设置的 renderView
	 */
	public void setRenderView(BLGFastRenderView renderView) {
		this.renderView = renderView;
	}
}
